<template>
  <div class="accountBookType">
    <div class="topHandleCon">
      <el-row :gutter="10">
        <el-col :span="8">
          <el-row
            :gutter="10"
            class="noBorderSelect"
          >
            <el-col :span="11">
              <el-card :body-style="{padding:0}">
                <el-row>
                  <el-col :span="8">
                    <span class="spanBorder time-title">共享类型</span>
                  </el-col>
                  <el-col :span="16">
                    <el-select
                      v-model="search.shareType"
                      size="small"
                      @change="searchFn"
                    >
                      <el-option
                        v-for="item of shareTypeList"
                        :key="item.value"
                        :value="item.value"
                        :label="item.name"
                      />
                    </el-select>
                  </el-col>
                </el-row>
              </el-card>
            </el-col>
            <el-col :span="11">
              <el-card :body-style="{padding:0}">
                <el-row>
                  <el-col :span="8">
                    <span class="spanBorder time-title">状态</span>
                  </el-col>
                  <el-col :span="16">
                    <el-select
                      v-model="search.status"
                      size="small"
                      @change="searchFn"
                    >
                      <el-option
                        v-for="item of statusList"
                        :key="item.value"
                        :value="item.value"
                        :label="item.name"
                      />
                    </el-select>
                  </el-col>
                </el-row>
              </el-card>
            </el-col>
          </el-row>
        </el-col>
        <el-col
          :span="16"
          class="greenBtnW"
        >
          <el-row :gutter="10">
            <el-col
              class="txtR"
              :span="7"
            >
              <el-card :body-style="{padding:0}">
                <el-row :gutter="10">
                  <el-col :span="6">
                    <div class="time-title">发证日期</div>
                  </el-col>
                  <el-col :span="18">
                    <el-date-picker
                      v-model="search.pubData"
                      style="display: inline-block;border: none;"
                      size="small"
                      type="daterange"
                      align="right"
                      unlink-panels
                      range-separator="至"
                      start-placeholder="开始日期"
                      end-placeholder="结束日期"
                      @change="searchInfo()"
                    />
                  </el-col>
                </el-row>
              </el-card>
            </el-col>
            <el-col
              class="txtR"
              :span="7"
            >
              <search
                v-model="search.keys"
                :show-btn="false"
                placeholder="数据编码/名称"
                @btnClick="searchFn"
              />
            </el-col>
            <el-col
              class="txtR"
              :span="3"
            >
              <div>
                <div
                  class="greenBtn"
                  @click="showEditType('add')"
                >新增</div>
              </div>
            </el-col>
            <el-col
              class="txtR"
              :span="3"
            >
              <div>
                <div
                  class="greenBtn"
                  @click="showEditType('update')"
                >修改</div>
              </div>
            </el-col>
            <el-col
              class="txtR"
              :span="3"
            >
              <div>
                <div
                  class="greenBtn"
                  @click="deleteConfirm"
                >删除</div>
              </div>
            </el-col>

            <el-col
              class="txtR"
              :span="1"
            >
              <div class="fresh">
                <div @click="refresh">
                  <i class="el-icon-refresh" />
                </div>
              </div>
            </el-col>
          </el-row>
        </el-col>
      </el-row>
    </div>
    <div />
    <div class="tableCon paddingLeft">
      <div class="tableShadow">
        <el-table
          ref="multipleTable"
          height="100%"
          tooltip-effect="dark"
          :data="tableData"
          border
          style="width: 100%"
          @selection-change="handleSelectionChange"
        >
          <el-table-column
            width="80"
            header-align="center"
            align="center"
          >
            <template
              slot="header"
              slot-scope="scope"
            >
              <el-checkbox
                v-model="checkedAll"
                :newname="scope.$index "
                @change="checkedAllFn"
              >序号</el-checkbox>
            </template>
            <template slot-scope="scope">
              <el-checkbox
                v-model="scope.row.checked"
                @change="rowChange"
              >{{ scope.$index+1 }}</el-checkbox>
            </template>
          </el-table-column>
          <el-table-column
            v-for="(item,index) in columnList"
            :key="index"
            :prop="item.prop"
            :label="item.label"
            :width="item.width||''"
            header-align="center"
            align="center"
          />
        </el-table>
      </div>
      <div class="pageArea">
        <el-pagination
          :current-page="pageindex"
          :page-sizes="[20,30,50,100,150,200]"
          :page-size="pagesize"
          layout="total,sizes,prev,pager,next,jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
    <el-dialog
      v-if="editTypeShow"
      :close-on-click-modal="false"
      :title="editType==='add'?'新增类型':'修改类型'"
      custom-class="addUserCss"
      width="1100px"
      :visible.sync="editTypeShow"
      append-to-body
    >
      <edit-type
        :type="editType"
        :info="editTypeDetail"
        @close="editTypeClose"
      />
    </el-dialog>
  </div>
</template>

<script>
import { getDictList, getDictModuleList } from '@/api/dataCenter'
import search from '@/components/searchInput'
import editType from './editType'
export default {
  name: 'AccountBookType',
  components: {
    search,
    editType
  },
  data() {
    return {
      typeList: [], // 策略类型列表
      editType: '',
      editTypeDetail: null,
      editTypeShow: false,
      userInfo: [], // 选中的数据
      search: {
        shareType: '',
        status: ''
      },
      shareTypeList: [
        {
          value: '',
          name: '全部'
        },
        {
          value: '0',
          name: '公有类型'
        },
        {
          value: '1',
          name: '私有类型'
        }
      ],
      statusList: [
        {
          value: '',
          name: '全部'
        },
        {
          value: '0',
          name: '启用'
        },
        {
          value: '1',
          name: '停用'
        }
      ],
      tableData: [], // 表格数据
      checkedAll: false, // 全选
      columnList: [
        {
          prop: 'account',
          label: '类型编码'
        },

        {
          prop: 'realname',
          label: '类型名称'
        },
        {
          prop: 'mobilephone',
          label: '创建人'
        },
        {
          prop: 'mobilephone',
          label: '创建时间'
        },
        {
          prop: 'mobilephone',
          label: '共享类型'
        },
        {
          prop: 'mobilephone',
          label: '状态'
        }
      ],
      pageindex: 0,
      pagesize: 30,
      total: 0
    }
  },
  mounted() {
    this.refresh()
  },
  methods: {
    editTypeClose(data) {
      if (data) {
        console.log(data)
        // do something...
      } else {
        this.editTypeShow = false
      }
    },
    showEditType(type) {
      if (type == 'edit') {
        var checkList = this.tableData.filter(i => i.checked)
        if (checkList.length == 0) {
          this.$msgBox.error('请至少选择一条数据!')
          return
        } else if (checkList.length > 1) {
          this.$msgBox.error('最多选择一条数据!')
          return
        }
        this.editTypeDetail = checkList[0]
      }
      this.editType = type
      this.editTypeShow = true
    },
    searchFn() {
      const item = this.typeList.find(type => {
        return type.checked
      })
      const data = {
        // enabledmark:
        // this.search.enabledmark === 5 ? '' : this.search.enabledmark,
        // keys: this.search.value,
        // "order": "string",
        pageIndex: this.pageindex,
        pageSize: this.pagesize,
        // "sort": "string",
        dataName: this.search.value,
        encode: item.encode
      }
      this.getTableList(data)
    },
    handleSizeChange(val) {
      this.pagesize = val
      this.searchFn()
    },
    handleCurrentChange(val) {
      this.pageindex = val
      this.searchFn()
    },
    // 选择
    clickFn(list, item, func) {
      list.forEach(li => {
        this.$set(li, 'checked', false)
      })
      this.$set(item, 'checked', true)
      func()
    },
    checkedAllFn(bool) {
      // 全选
      this.tableData.forEach(tab => {
        this.$set(tab, 'checked', bool)
      })
    },
    rowChange() {
      // 改变行
      this.checkedAll = this.tableData.every(table => {
        return table.checked
      })
    },

    // 删除确认
    deleteConfirm() {
      this.userInfo = []
      this.tableData.forEach(tab => {
        if (tab.checked) {
          this.userInfo.push(tab)
        }
      })
      if (this.userInfo.length === 0) {
        this.$msgBox.error('请选择要修改的用户!')
        return
      }
      this.$confirm(
        '删除用户后，该账号将不能继续使用本系统进行业务操作，确定继续删除吗？',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      )
        .then(async() => {
          this.deleteUser(
            this.userInfo.map(info => {
              return info.phid
            })
          )
        })
        .catch(err => {
          console.error(err)
        })
    },
    // 删除
    deleteUser(data) {
      // getUserDelete(data).then(res => {
      //   if (res.status) {
      //     this.$msgBox.error('删除成功!')
      //     this.searchFn()
      //   } else {
      //     this.$msgBox.error(res.message)
      //   }
      // }).catch(err => {
      //   console.log(err)
      //   this.$msgBox.error('删除失败!')
      // })
    },
    refresh() {
      this.getData()
    },
    getData(data) {
      getDictModuleList(data).then(res => { // 类型列表
        if (res.status) {
          this.typeList = res.data
          this.typeList.forEach((item, i) => {
            if (i === 0) {
              this.$set(item, 'checked', true)
            } else {
              this.$set(item, 'checked', false)
            }
          })
          this.searchFn()
        } else {
          this.$msgBox.error(res.message)
        }
      }).catch(err => {
        console.log(err)
        this.$msgBox.error('获取用户列表失败!')
      })
    },
    getTableList(data) { // 列表数据
      getDictList(data).then(res => {
        if (res.status) {
          this.tableData = res.data.records
        } else {
          this.$msgBox.error(res.message)
        }
      }).catch(err => {
        console.log(err)
      })
    },
    finishAdd(bool) { // 完成或取消新增修改 ,true完成
      if (bool) {
        this.searchFn()
      }
      this.addDataType = false
    },
    handleSelectionChange() { }
  }
}
</script>

<style lang="scss" scoped>
@import "@/styles/variables.scss";
.accountBookType {
  min-width: 1250px;
  height: 100%;
  padding: 8px 0;
  overflow: hidden;
  position: relative;
}
.topHandleCon {
  padding: 0 20px;
  margin-top: 5px;
  .time-title {
    width: 100%;
    margin-top: 7px;
    text-align: center;
    border-right: 1px solid #cccccc;
  }
}
.noBorderSelect >>> .el-select {
  width: 100%;
  input {
    border: 0;
  }
}
.spanBorder {
  display: inline-block;
  width: 100%;
  height: 100%;
  line-height: 20px;
  margin-top: 4px;
  text-align: center;
  border-right: 1px solid $borderGray;
}
.greenBtnW {
  text-align: right;
  .greenBtn {
    margin: 0;
    min-width: 100%;
  }
  .fresh {
    height: 30px;
    line-height: 30px;
    font-size: 22px;
    text-align: center;
    cursor: pointer;
    color: #666;
  }
}
.tableCon {
  padding: 20px;
  padding-bottom: 70px;
  height: 100%;
  // position: relative;
  >>> .el-table--border {
    border: 0;
  }
  >>> .el-table::before {
    height: 0;
  }
  >>> .el-table::after {
    width: 0;
  }
  >>> .has-gutter tr > th:nth-last-of-type(2) {
    border-right: 0;
  }
}
.tableShadow {
  width: 100%;
  height: 100%;
  padding: 8px;
  box-shadow: 0 0 5px 3px #ddd;
  border-radius: 5px;
  overflow: hidden;
}
.pageArea {
  position: absolute;
  right: 90px;
  bottom: 5px;
}
</style>
<style lang="scss">
@import "@/styles/variables.scss";
.addUserCss {
  .el-dialog__header {
    background: $green;
    .el-dialog__title {
      color: #fff;
    }
  }
  border-radius: 10px;
  overflow: hidden;
}
</style>
<style scoped>
.accountBookType >>> .el-range-editor.el-input__inner {
  width: 300px;
  border: 0;
}
.accountBookType >>> .el-range-editor--small .el-range-separator {
  padding: 0;
}

.accountBookType >>> .el-range-editor--small .el-range-input {
  width: 100px;
}
</style>
